import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login/Login.vue')
  },
  {
    path: '/overtime/:id',
    name: 'overtime',
    component: () => import(/* webpackChunkName: "about" */ '../views/Overtime/Overtime.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import(/* webpackChunkName: "about" */ '../views/Detail/Detail.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//注册一个全局导航守卫
router.beforeEach((to,from,next) => {
    //判断是否已经登陆
    console.log('beforeach');
    var token = localStorage.getItem('token');
    if (token)//已登录状态
    {
      if (to.path === '/login')
      {
        next('/home');//重定向到 /home
      }
      else
      {
        next();//渲染页面
      }
    }
    else//未登录状态
    {
      if (to.path === '/login') {
        next();//渲染页面
      }
      else {
        next('/login');//重定向到 /login
      }
    }
  
})

export default router
